<template>
  <a-modal :title="`编辑普通款`" style="top: 100px" :visible="true" width="1560px" @cancel="handleCancel">
    <div class="tips" style="color: red; font-size: 96%; margin-bottom: 10px">
      虚拟资产类奖品禁止回收
      <a href="https://www.yuque.com/u1307645/mwy3k3/xkcbir" target="_blank">回收说明文档</a>
    </div>
    <a-table
      class="product-table"
      :rowKey="(row) => row.id"
      :dataSource="list"
      bordered
      :pagination="false"
      size="small"
    >
      <a-table-column title="序号" width="50px">
        <template slot-scope="text, record, index">
          <div>{{ index + 1 }}</div>
        </template>
      </a-table-column>
      <a-table-column title="图片" width="130px">
        <template slot-scope="row">
          <UploadImages v-model="row.thumb" tips="600*800" size="small"></UploadImages>
        </template>
      </a-table-column>
      <!-- <a-table-column title="标题/编号" width="220px"> -->
      <a-table-column title="标题" width="220px">
        <template slot-scope="row">
          <div style="margin-top: 6px; display: flex; align-items: center">
            <div style="flex: 0 0 36px">标题</div>
            <a-input style="width: 160px" v-model="row.title" placeholder="请输入" />
          </div>
          <!-- <div style="margin-top: 6px; display: flex; align-items: center;">
            <div style="flex: 0 0 36px">编号</div>
            <a-input v-model="row.number" placeholder="选填" />
          </div> -->
        </template>
      </a-table-column>
      <a-table-column title="SKU编号" width="180px">
        <template slot-scope="row">
          <div style="margin-top: 6px; display: flex; align-items: center">
            <div style="flex: 0 0 36px">SKU</div>
            <a-input style="width: 120px" v-model="row.serial_number" placeholder="请输入" />
          </div>
          <!-- <div style="margin-top: 6px; display: flex; align-items: center;">
            <div style="flex: 0 0 36px">编号</div>
            <a-input v-model="row.number" placeholder="选填" />
          </div> -->
        </template>
      </a-table-column>
      <!-- <a-table-column title="奖品类型" width="130px">
        <template slot-scope="row">
          <ISkuType :info="row"></ISkuType>
          <a href="javascript:;" @click="handleEdit(row)">编辑</a>
        </template>
      </a-table-column> -->
      <!-- <a-table-column title="编号" key="number" width="150px">
        <template slot-scope="row">
          <div>
            <a-input v-model="row.number" placeholder="请输入"/>
          </div>
        </template>
      </a-table-column> -->
      <a-table-column width="160px">
        <div slot="title">
          显示价
          <tips>1、仅在小程序端作显示使用</tips>
        </div>
        <template slot-scope="row">
          <div>
            <IPrice v-model="row.display_money_price" size="small" placeholder="请输入" style="width: 110px" />
          </div>
          <!-- <div style="margin-top: 6px;">
            <a-input v-model.number="row.display_score_price" size="small" placeholder="请输入" style="width: 90px" />&nbsp积分
          </div> -->
        </template>
      </a-table-column>
      <!-- <a-table-column width="160px">
        <div slot="title">
          回收价
          <tips>1、作为回收原价进行折扣计算</tips>
        </div>
        <template slot-scope="row">
          <div>
            <IPrice v-model="row.money_price" size="small" placeholder="请输入" style="width: 110px" /> +
          </div>
          <div style="margin-top: 6px;">
            <a-input v-model.number="row.score_price" size="small" placeholder="请输入" style="width: 90px" />&nbsp积分
          </div>
        </template>
      </a-table-column> -->
      <!-- <a-table-column title="库存" width="90px">
        <template slot-scope="row">
          <span>
            <a-input v-model="row.stock" placeholder="请输入" />
          </span>
        </template>
      </a-table-column> -->
      <a-table-column title="归属IP" width="220px">
        <template slot-scope="row">
          <a-select :defaultValue="''"  style="width: 100%" v-model="row.category_id" @change="handleChangeIP($event,row)">
            <!-- <template v-for="item in categoryList">
                <a-select-option :key="item.id">
                  <span slot="label">{{ item.title }}</span>
                </a-select-option>
              </template> -->
            <a-select-option v-for="item in categoryList" :key="item.id" :value="item.id">
              {{ item.title}}
            </a-select-option>
          </a-select>
        </template>
      </a-table-column>
      <a-table-column title="归属人物" width="220px">
        <template slot-scope="row">
          <div>
            <a-select v-model="row.character_id"  style="width: 100%" @change="changeCharacterid($event,row)">
              <template v-for="item in characterList">
                <a-select-opt-group :key="item.id">
                  <span slot="label">{{ item.title }}</span>
                  <a-select-option :value="sub.id" v-for="sub in item.sub_categories" :key="sub.id">
                    {{ sub.title }}
                  </a-select-option>
                </a-select-opt-group>
              </template>
            </a-select>
          </div>
        </template>
      </a-table-column>
      <a-table-column width="120px">
        <div slot="title">
          抽中概率(%)
          <tips
            >1、此概率在连抽情况或非1:1情况下生效 <br />
            2、此概率可设置显示在小程序端</tips
          >
        </div>
        <template slot-scope="row">
          <span> <a-input v-model="row.odds" placeholder="0-100" style="width: 70px; margin: 0px 1px" />% </span>
        </template>
      </a-table-column>
      <a-table-column title="排序" width="100px">
        <template slot-scope="row">
          <div>
            <span>{{ row.list_weight || 0 }}&nbsp;</span>
            <a-popover title="排序" trigger="click" v-model="row.visible">
              <template slot="content">
                <div style="display:flex;">
                  <a-input v-model="listWeight" placeholder="请输入" autofocus style="margin-right:10px;width:120px;" size="small"></a-input>
                  <a-button type="primary" size="small" @click="handleUpdateListWeight(row)" :loading="loading3">保存</a-button>
                </div>
              </template>
              <span><a href="javascript:;">
                  <a-icon type="edit" /></a></span>
            </a-popover>
          </div>
        </template>
      </a-table-column>
      <!-- <a-table-column width="80px">
        <div slot="title">
          等级
          <tips>1、此等级仅用于标记是否显示在开盒榜单中</tips>
        </div>
        <template slot-scope="row">

          <a-select class="select" v-model="row.level" size="small" style="width: 80px;">
            <a-select-option :value="1">等级1</a-select-option>
            <a-select-option :value="2">等级2</a-select-option>
            <a-select-option :value="3">等级3</a-select-option>
            <a-select-option :value="4">等级4</a-select-option>
            <a-select-option :value="5">等级5</a-select-option>
          </a-select>

        </template>
      </a-table-column> -->

      <!-- <a-table-column title="已售" width="80px">
        <template slot-scope="row">
          <span>
            {{row.sales}}次
          </span>
        </template>
      </a-table-column>
      <a-table-column title="允许回收" width="80px">
        <template slot-scope="row">
          <span v-if="row.sku_type === 3">
            系统禁止
          </span>
          <span v-else>
            <i-switch :active="row.is_return_saleable || 1" v-model="row.is_return_saleable" size="small"></i-switch>
          </span>
        </template>
      </a-table-column>
      <a-table-column title="上下架" width="80px">
        <template slot-scope="row">
          <span>
            <i-switch :active="row.status || 1" v-model="row.status" size="small"></i-switch>
          </span>
        </template>
      </a-table-column> -->
      <a-table-column title="操作" width="100px">
        <template slot-scope="row">
          <!-- <a href="javascript:;">编辑</a>
          <a-divider type="vertical" /> -->
          <a href="javascript:;" @click="submitList(row)">保存</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <!-- <a-button type="primary" class="add" @click="handleCreate">
          新增一款
        </a-button> -->
        <div style="flex-grow: 1"></div>
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button key="submit" type="primary" @click="handleSave"> 批量保存 </a-button>
      </div>
    </template>
  </a-modal>
</template>
<script>
import UploadImages from '@/components/UploadImages/UploadImages'
import iSwitch from '@/components/ISwitch/ISwitch'
import IPrice from '@/components/IForm/components/IPrice'
import ISkuType from '@/components/ISkuType/ISkuType'
export default {
  components: {
    UploadImages,
    iSwitch,
    IPrice,
    ISkuType,
  },
  props: {
    uuid: {
      type: String,
      default() {
        return ''
      },
    },
    characterList:{
        type:Array,
        default(){
          return []
        }
      },
      categoryList:{
        type:Array,
        default(){
          return []
        }
      },
  },
  data() {
    return {
      category_id:null,
      character_id:null,
      listWeight:0,
      list: [],
      frame: [
        {
          showPop: false,
          list_weight: 0,
          // categoryList: [],
          // characterList: [],
          title: '奖品类型',
          key: 'sku_type',
          type: 'radio',
          default: 1,
          required: true,
          span: 24,
          options: [
            {
              title: '无绑定',
              value: 1,
            },
            {
              title: '绑定商城中的商品',
              value: 2,
            },
            {
              title: '平台虚拟资产',
              value: 3,
            },
          ],
          tips: '',
        },
        {
          title: '绑定商品',
          key: 'product_id',
          type: 'product_id',
          auto_fill_fields: {
            title: 'title',
            thumb: 'thumb',
            number: 'number',
            money_price: 'money_price',
            score_price: 'score_price',
          },
          where: {
            sku_type: 2,
          },
        },
        // {
        //   title: '操作',
        //   type: 'button',
        //   text: '点击获取商品信息',
        //   where: {
        //     sku_type: 2
        //   }
        // },
        {
          title: '绑定虚拟资产',
          key: 'assets',
          type: 'assets',
          where: {
            sku_type: 3,
          },
        },
        {
          title: '奖品名称',
          key: 'title',
          required: true,
          placeholder: '如"积分礼包1"',
        },
        {
          title: '编号',
          key: 'number',
          placeholder: '可填写编号方便在订单中对应货物',
          // required: true,
        },
        {
          title: '封面',
          key: 'thumb',
          type: 'image',
        },
        {
          title: '奖品详情图',
          key: 'detail_images',
          type: 'images',
          newline: true,
          sizeLimit: 400,
          tips: '显示在用户的物品详情中。 不上传则不显示。 <br/> 长图请切成多张小图上传',
          // tips: '不设置则没有连'
          // required: true
        },
      ],
    }
  },
  watch: {
    uuid() {
      this.initList()
    },
  },
  created() {
    this.initList()
    // this.getcategories()
   
  },
  mounted() {},
  methods: {
    changeCharacterid(e,row){
      // this.character_id = e
      row.character_id = e
      console.log(row)
    },
    handleChangeIP(e,row){
      console.log(e)
      
      row.category_id=e
      console.log(row)
    },
    // 保存单个
    submitList(row){
      // console.log(row)
      this.$http.post('/carddrawersku/update',{
        id:row.id,
        title:row.title,
        thumb:row.thumb,
        card_drawer_id:row.card_drawer_id,
        odds:row.odds,
        display_money_price:row.display_money_price,
        list_weight:row.list_weight,
        character_id:row.character_id,
        category_id:row.category_id,
        serial_number:row.serial_number
      }).then(res=>{
        // console.log(res)
        this.$message.success('保存成功')
      })
    },
    handleUpdateListWeight(row) {
      row.visible=false
      row.list_weight=this.listWeight
    },
   
    handleEdit(row) {
      this.$iForm.show({
        title: '编辑活动奖品',
        frame: this.frame,
        form: row,
        success: (data) => {
          let index = this.list.findIndex((item) => {
            return item.uuid === row.uuid
          })
          console.log(index)
          this.list.splice(index, 1, {
            ...this.list[index],
            ...data,
          })
          // this.list[index] = {
          //   ...this.list[index],
          //   ...data
          // }
          this.$iForm.hide()
          // this.$http.put(`/fudai/skus/${row.uuid}`, {
          //   type: 'update',
          //   attributes: {
          //     ...data
          //   }
          // }).then(res => {
          //   this.initList()
          //   this.$iForm.hide()
          // })
        },
      })
    },
    initList() {
      this.$http
        .get(`/carddrawersku/index`, {
          params: {
            card_drawer_id: this.uuid,
          },
        })
        .then((res) => {
          console.log('res', res)
          this.list = res.data.list
        })
    },
    //新增
    // handleCreate() {
    //   this.$http.post(`/boxes/${this.uuid}/normal-skus`).then(res => {
    //     this.initList()
    //   })
    // },
    //取消
    handleCancel() {
      this.$emit('refresh')
      this.$emit('close')
    },
    // 保存
    handleSave() {
      console.log('list',this.list)
      this.$http
        .post(`/carddrawersku/batchupdate`, {
          list: this.list,
        })
        .then((res) => {
          this.$message.success('保存成功')
          this.initList()
        })
    },
    handleDelete(row) {
      this.$http.delete(`/boxes/${this.uuid}/normal-skus/${row.uuid}`).then((res) => {
        this.$message.success('删除成功')
        this.initList()
      })
    },
  },
}
</script>
<style lang="less" scoped>
.btn-c {
  display: flex;
  // margin-top: 30px;
  padding: 0rpx 30rpx;

  .add {
  }

  .save {
    margin-right: 20px;
  }

  .cancel {
  }
}

a-input {
  display: inline;
}
</style>
